﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using OnTheWay.Functions;
using Models;
using System.Reflection;
using System.ComponentModel;


namespace PcBackgroundMgr
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeFunctioin();
            InitializeComponent();
        }

        private void InitializeFunctioin()
        {
            //Functions = new List<Function>();
            Functions.Clear();
            if (Admin.IsSuper)
            {
                Functions.AddRange(FunctionFactory.SuperFunctions);
            }
            else
            {
                Functions.AddRange(FunctionFactory.Functions);
            }
        }

        public SysAdmin Admin
        {
            get { return BlackMofoApp.App.Admin; }
        }

        public List<Function> Functions //{get;set;}
        {
            get { return (List<Function>)GetValue(FunctionsProperty); }
            set { SetValue(FunctionsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Functions.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FunctionsProperty =
            DependencyProperty.Register("Functions", typeof(List<Function>), 
            typeof(MainWindow), new UIPropertyMetadata(new List<Function>()));

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            if (sender is RadioButton)
            {
                RadioButton button = sender as RadioButton;
                if (button.DataContext is Function)
                {
                    Function function = button.DataContext as Function;
                    Assembly assembly = Assembly.GetExecutingAssembly();
                    mFunctionPanel.StartFunction(function, assembly);
                }
            }
        }

        #region 关闭和注销

        public bool mIsLogout { get; set; }

        protected override void OnClosing(CancelEventArgs e)
        {
            mFunctionPanel.OnClosing(e);
            if (mIsLogout && e.Cancel == true)
            {
                mIsLogout = false;
            }
            if (mIsLogout)
            {
                BlackMofoApp.App.Admin = null;
                LoginWindow login = new LoginWindow();
                login.Show();
            }
        }

        protected override void OnClosed(EventArgs e)
        {
            mFunctionPanel.OnClosed(e);
        }

        private void mbtLogout_Click(object sender, RoutedEventArgs e)
        {
            mIsLogout = true;
            this.Close();
        }

        #endregion


    }
}
